<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    append-to-body
    :close-on-click-modal="false"
    @close="closeDialog()"
    :visible="visible"
    width="1200px"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="120px"
    >
      <el-form-item label="礼品名称" prop="giftName">
        <el-input
          v-model="dataForm.giftName"
          placeholder="请输入礼品名称"
        ></el-input>
      </el-form-item>

      <el-form-item label="轮播图" prop="bannerImages">
        <ImagesUpload v-model="dataForm.bannerImages"></ImagesUpload>
      </el-form-item>

      <el-form-item label="详情图" prop="detailImages">
        <ImagesUpload v-model="dataForm.detailImages"></ImagesUpload>
      </el-form-item>

      <el-row>
        <el-col :span="12">
          <el-form-item label="品牌" prop="brand">
            <el-input v-model="dataForm.brand" placeholder="请输入品牌" ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="规格" prop="specs">
            <el-input v-model="dataForm.specs" placeholder="请输入规格" ></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="尺寸" prop="size">
            <el-input v-model="dataForm.size" placeholder="请输入尺寸" ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="颜色" prop="color">
            <el-input v-model="dataForm.color" placeholder="请输入颜色" ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      
      <el-row>
        <el-col :span="12">
          <el-form-item label="上架状态" prop="status">
            <el-radio-group v-model="dataForm.status" style="width:300px">
              <el-radio label="1" style="margin-right:10px" border>上架</el-radio>
              <el-radio label="0" style="margin-right:10px" border>下架</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="发货类型" prop="sendType">
            <el-radio-group v-model="dataForm.sendType" style="width:300px">
              <el-radio label="1" style="margin-right:10px" border>单品</el-radio>
              <el-radio label="0" style="margin-right:10px" border>批量</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
      
      <el-form-item label="备注" prop="remark">
        <el-input type="textarea" :rows="4" v-model="dataForm.remark" placeholder="备注"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="closeDialog">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" v-if="canSubmit"
        >确定</el-button
      >
    </span>
  </el-dialog>
</template>

<script>
import { getObj, addObj, putObj } from "@/api/shop/goods/gift";
import ImagesUpload from "@/components/imagesUpload";

export default {
  props:{
    visible: {
      type: Boolean,
      default: false
    },
  },
  data() {
    return {
      canSubmit: false,
      dataForm: {
        status:'1',
        sendType:'1',
        bannerImages:[],
        detailImages:[]
      },
      dataRule: {
        giftName: [
          { required: true, message: "礼品名称不能为空", trigger: "blur" },
        ],
        bannerImages: [
          { required: true, message: "轮播图不能为空", trigger: "blur" },
        ],
        detailImages: [
          { required: true, message: "详情图不能为空", trigger: "blur" },
        ],
        status: [
          { required: true, message: "上架不能为空", trigger: "blur" },
        ],
        sendType: [
          { required: true, message: "发货类型不能为空", trigger: "blur" },
        ]
      },
    };
  },
  components: {
    ImagesUpload,
  },
  methods: {
    init(id) {
      this.canSubmit = true;
      if(id){
        getObj(id).then((response) => {
          let data = response.data.data;
          data.bannerImages = data.bannerImages ? JSON.parse(data.bannerImages) : []
          data.detailImages = data.detailImages ? JSON.parse(data.detailImages) : []
          this.dataForm = data
          console.log(this.dataForm)
        });
        
      }
      
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          let requestData = JSON.parse(JSON.stringify(this.dataForm))
          requestData.bannerImages = requestData.bannerImages.map(p=>p.materialId).join()
          requestData.detailImages = requestData.detailImages.map(p=>p.materialId).join()
          console.log(requestData);
          this.canSubmit = false;
          if (requestData.id) {
            putObj(requestData)
              .then((data) => {
                this.$notify.success("修改成功");
                this.$emit("refreshDataList");
                this.closeDialog()
              })
              .catch(() => {
                this.canSubmit = true;
              });
          } else {
            addObj(requestData)
              .then((data) => {
                this.$notify.success("添加成功");
                this.$emit("refreshDataList");
                this.closeDialog()
              })
              .catch(() => {
                this.canSubmit = true;
              });
          }
        }
      });
    },
    //重置表单
    closeDialog() {
      this.$emit('update:visible', false)
    },
  },
};
</script>
